package com.study.service.impl;

import com.study.exception.ServiceException;
import com.study.model.dto.request.SendSmsDto;
import com.study.model.dto.request.SendVerifyCodeDto;
import com.study.service.SendSmsService;
import com.study.util.SysConst;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

//视频第一节 21:10

@Slf4j
@Service
public class SendSmsServiceImpl implements SendSmsService {


    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void sendVerifyCode(SendVerifyCodeDto sendVerifyCodeDto) {
        //60秒内是否发过验证码
        RBucket<String> verifyCodeSendCache = redissonClient.getBucket(SysConst.RedisPrefix.SEND_VERIFY_CODE_60+sendVerifyCodeDto.getPhone());
        //手机号和验证码的映射
        RBucket<String> verifyCodeCache = redissonClient.getBucket(SysConst.RedisPrefix.SEND_VERIFY_PHONE_MAP+sendVerifyCodeDto.getPhone());

        //1.根据手机号判断60s内是否发过
        if (verifyCodeSendCache.isExists()) {
            log.error("{} 频繁请求验证码，已被拒绝", sendVerifyCodeDto.getPhone());
            throw new ServiceException("请不要频繁请求验证码");
        }

        //2. 生成随机的六位数
        String randomStr = RandomStringUtils.randomNumeric(6);
        log.info("短信验证码：{}", randomStr);

        //3. 调用API发送
        //smsApi.sendVerifyCode(sendVerifyCodeDto.getPhone(), randomStr);
        //String message = "【扣拉科技】您的验证码是" + randomStr + "，验证码在10分钟内有效。";
        //rabbitTemplate.convertAndSend("sms_queue", new SendSmsDto(sendVerifyCodeDto.getPhone(), message));

        //3. 根据场景值 调用API发送验证码
        if (sendVerifyCodeDto.getSceneCode() == SendVerifyCodeDto.SCENE_CODE_REGISTER) {
            sendRegisterCode(randomStr, sendVerifyCodeDto.getPhone());
        } else if (sendVerifyCodeDto.getSceneCode() == SendVerifyCodeDto.SCENE_CODE_FORGET_PASSWORD) {
            sendForgetPasswordCode(randomStr, sendVerifyCodeDto.getPhone());
        }

        verifyCodeSendCache.set(randomStr, 60, TimeUnit.SECONDS);
        verifyCodeCache.set(randomStr, 10, TimeUnit.MINUTES);

    }


    private void sendForgetPasswordCode(String randomStr, String phone) {

        RBucket<String> rBucket = redissonClient.getBucket(SysConst.RedisPrefix.FORGET_PASSWORD_SEND_VERIFY + phone);
        if(!rBucket.isExists()) {
            throw new ServiceException("");
        }
        String phoneNumber = rBucket.get();
        String message = "【JOJO科技】您找回密码的验证码是" + randomStr + "，验证码在10分钟内有效。";

        rabbitTemplate.convertAndSend(SysConst.MqQueueName.SMS_QUEUE, new SendSmsDto(phoneNumber, message));
    }

    private void sendRegisterCode(String str, String phone) {
        String message = "【JOJO科技】您注册的验证码是" + str + "，验证码在10分钟内有效。";
        rabbitTemplate.convertAndSend(SysConst.MqQueueName.SMS_QUEUE, new SendSmsDto(phone, message));
    }


}
